// pages/productRepository/productRepository.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //图标
    iconPath: app.globalData.iconPath,
    //访问java后台
    goJavaBackstagePath: app.globalData.goJavaBackstagePath,
    //测试数据图片
    testDataImgPath: app.globalData.testDataImgPath,

    typeProductList:[],
    selectTypeId:null,
    selectProductList:[],   
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {  
    var orderId = options.orderId;
    this.setData({
      orderId:orderId,
    });
    //--------------------------------------------发送请求到后台获取照片库页面数据
    // wx.request({
    //   url: this.data.goJavaBackstagePath+'/productType/getProductRepository',
    //   data:{},
    //   header: {
    //     'content-type': 'application/json'
    //   },
    //   success: function (res) {
    //     console.log(res.data)
    //   }
    // })


    var typeProductList = [{
      id: 1, name: '相框', products: [
        { id: 1, url: this.data.testDataImgPath+ '/mei1.jpg', name: '相框产品一', price: 120 },
        { id: 2, url: this.data.testDataImgPath+ '/mei2.jpg', name: '相框产品二', price: 110 },
        { id: 3, url: this.data.testDataImgPath+ '/mei3.jpg', name: '相框产品三', price: 130 },
        { id: 19, url: this.data.testDataImgPath+ '/mei1.jpg', name: '相框产品四', price: 120 },
        { id: 20, url: this.data.testDataImgPath+ '/mei2.jpg', name: '相框产品五', price: 110 },
        { id: 21, url: this.data.testDataImgPath+ '/mei3.jpg', name: '相框产品六', price: 130 },
      ]
    }, {
      id: 2, name: '海报', products: [
        { id: 4, url: this.data.testDataImgPath+ '/mei4.jpg', name: '海报产品一', price: 80 },
        { id: 5, url: this.data.testDataImgPath+ '/mei5.jpg', name: '海报产品二', price: 90 },
        { id: 6, url: this.data.testDataImgPath+ '/mei6.jpg', name: '海报产品三', price: 100 },
        { id: 22, url: this.data.testDataImgPath+ '/mei4.jpg', name: '海报产品四', price: 80 },
        { id: 23, url: this.data.testDataImgPath+ '/mei5.jpg', name: '海报产品五', price: 90 },
        { id: 24, url: this.data.testDataImgPath+ '/mei6.jpg', name: '海报产品六', price: 100 },
      ]
    }, {
      id: 3, name: '相册', products: [
        { id: 7, url: this.data.testDataImgPath+ '/mei7.jpg', name: '相册产品一', price: 50 },
        { id: 8, url: this.data.testDataImgPath+ '/mei8.jpg', name: '相册产品二', price: 60 },
        { id: 9, url: this.data.testDataImgPath+ '/mei9.jpg', name: '相册产品三', price: 30 },
        { id: 13, url: this.data.testDataImgPath+ '/mei7.jpg', name: '相册产品四', price: 50 },
        { id: 14, url: this.data.testDataImgPath+ '/mei8.jpg', name: '相册产品五', price: 60 },
        { id: 15, url: this.data.testDataImgPath+ '/mei9.jpg', name: '相册产品六', price: 30 },
        { id: 16, url: this.data.testDataImgPath+ '/mei7.jpg', name: '相册产品七', price: 50 },
        { id: 17, url: this.data.testDataImgPath+ '/mei8.jpg', name: '相册产品八', price: 60 },
        { id: 18, url: this.data.testDataImgPath+ '/mei9.jpg', name: '相册产品九', price: 30 },
      ]
    }, {
      id: 4, name: '钥匙扣', products: [
        { id: 10, url: this.data.testDataImgPath+ '/mei10.jpg', name: '钥匙扣产品一', price: 10 },
        { id: 11, url: this.data.testDataImgPath+ '/mei11.jpg', name: '钥匙扣产品二', price: 20 },
        { id: 12, url: this.data.testDataImgPath+ '/mei12.jpg', name: '钥匙扣产品三', price: 30 },
        { id: 25, url: this.data.testDataImgPath+ '/mei10.jpg', name: '钥匙扣产品四', price: 10 },
        { id: 26, url: this.data.testDataImgPath+ '/mei11.jpg', name: '钥匙扣产品五', price: 20 },
        { id: 27, url: this.data.testDataImgPath+ '/mei12.jpg', name: '钥匙扣产品六', price: 30 },
      ]
    }];
    this.setData({ typeProductList: typeProductList});
   
  },
  triggerEvent:function(){},
  openType: function (event) {
    var selectTypeId = event.target.dataset.typeid;
    if (this.data.selectTypeId == selectTypeId){
      selectTypeId=-1;
    }
    this.setData({ selectTypeId: selectTypeId });
  },
  selectProduct:function(event){
    var productid=event.target.dataset.productid;
    this.circleProduct(productid, this.selectP);

      //添加产品到后台
    // wx.request({
    //   url: this.data.goJavaBackstagePath+'/product/addProduct',
    //   data:{orderId:this.data.orderId,productId:productid},
    //   header: {
    //     'content-type': 'application/json'
    //   },
    //   success: function (res) {//返回为1 有异常  返回为0正常
    //     console.log(res.data)
    //   }
    // })
  },
  unSelectProduct:function(event){
    var productid = event.target.dataset.productid;
    this.circleProduct(productid, this.unSelectP);
    //删除产品到后台
    // wx.request({
    //   url: this.data.goJavaBackstagePath+'/product/deleteProduct',
    //   data:{orderId:this.data.orderId,productId:productid},
    //   header: {
    //     'content-type': 'application/json'
    //   },
    //   success: function (res) {//返回为1 有异常  返回为0正常
    //     console.log(res.data)
    //   }
    // })
  },
  //跳转到订单详情页面
  toProduct: function () {
    wx.redirectTo({ url: '/pages/product/product?orderId='+this.data.orderId })
  },
  //跳转到照片库页面
  toPhotoRepository: function () {
    wx.redirectTo({ url: '/pages/photoRepository/photoRepository?orderId='+this.data.orderId })
  },
  previewProduct(event){
    var current=event.target.dataset.current;
    wx.previewImage({current:current,urls:[current]})
  },

  /** 遍历找到要添加或要删除的产品进行操作 **/
  circleProduct:function(productid,fun){
    for (var i = 0; i < this.data.typeProductList.length;i++){
      if (this.data.selectTypeId == this.data.typeProductList[i].id){
        var products = this.data.typeProductList[i].products;
        for(var j=0;j<products.length;j++){
          if (productid == products[j].id){
            fun(products[j], this.data.typeProductList[i]);
          }
        }
      }
    }
    this.setData({ typeProductList: this.data.typeProductList});
  },
  /** 选择产品 **/
  selectP:function(product,type){
    product.checked=true;
    this.data.selectProductList.push(product.id);
    if (type.selectProduct==null){
      type.selectProduct=[];
    }
    var selectp={id:product.id,name:product.name};
    type.selectProduct.push(selectp);  
  },
  /** 取消选择产品 **/
  unSelectP: function (product,type) {
    product.checked = false;
    var removeindex=-1;
    for (var i = 0; i < this.data.selectProductList.length;i++){
      if (this.data.selectProductList[i]==product.id){
        removeindex=i;
      }
    }
    this.data.selectProductList.splice(removeindex,1);
    var removeTpIndex=-1;
    for (var i = 0; i < type.selectProduct.length;i++){
      if (product.id == type.selectProduct[i].id){
        removeTpIndex=i;
      }
    }
    type.selectProduct.splice(removeTpIndex,1);
  },
})